package org.example.controller;

import org.example.common.ResponseEntity;

import org.example.service.VideoParseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class VideoParseRecordController {
    
    @Autowired
    private VideoParseRecordService videoParseRecordService;

    @GetMapping("/video-parse-platforms")
    public ResponseEntity<List<String>> getAllPlatforms() {
        try {
            List<String> platforms = videoParseRecordService.getAllPlatforms();
            return ResponseEntity.success(platforms);
        } catch (Exception e) {
            return ResponseEntity.fail("获取平台列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/video-parse-nicknames")
    public ResponseEntity<List<String>> getAllNicknames() {
        try {
            List<String> nicknames = videoParseRecordService.getAllNicknames();
            return ResponseEntity.success(nicknames);
        } catch (Exception e) {
            return ResponseEntity.fail("获取用户列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/video-parse-records")
    public ResponseEntity<Map<String, Object>> getVideoParseRecords(
            @RequestParam(value = "nickname", required = false) String nickname,
            @RequestParam(value = "platform", required = false) String platform,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        try {
            // 确保空字符串转为 null
            String finalNickname = (nickname != null && nickname.trim().isEmpty()) ? null : nickname;
            String finalPlatform = (platform != null && platform.trim().isEmpty()) ? null : platform;
            String finalKeyword = (keyword != null && keyword.trim().isEmpty()) ? null : keyword;

            Map<String, Object> result = videoParseRecordService.getVideoParseRecords(
                    finalNickname, finalPlatform, finalKeyword, page, pageSize
            );

            System.out.println("finalPlatform = " + finalPlatform);
            System.out.println("finalNickname = " + finalNickname);
            return ResponseEntity.success(result);
        } catch (Exception e) {
            return ResponseEntity.fail("获取数据失败: " + e.getMessage());
        }
    }
}